package com.suguowen.sblog.mapper;

import com.suguowen.sblog.bean.Comment;
import com.suguowen.sblog.bean.CommentExample;
import java.util.List;

import com.suguowen.sblog.bean.vo.CommonVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface CommentMapper {
    long countByExample(CommentExample example);

    int deleteByExample(CommentExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Comment record);

    @Insert("insert into comment (time, content,user_id,article_id,father_comment) values(#{time},#{content},#{userId},#{articleId},#{fatherComment})")
    @Options(useGeneratedKeys=true,keyProperty = "id", keyColumn = "id")
    int insertAutoId(Comment record);

    int insertSelective(Comment record);

    List<Comment> selectByExample(CommentExample example);

    Comment selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Comment record, @Param("example") CommentExample example);

    int updateByExample(@Param("record") Comment record, @Param("example") CommentExample example);

    int updateByPrimaryKeySelective(Comment record);

    int updateByPrimaryKey(Comment record);

    @Select("select c.id,c.time,c.content,c.user_id as sender_id,\n" +
            "  a.username as sender_name,\n" +
            "  c.article_id,\n" +
            "  father_comment as receiver_id,\n" +
            "  b.username as receiver_name,\n" +
            "  d.title as article_name\n" +
            "from user a, user b ,comment c,article d\n" +
            "where a.id = c.user_id and b.id = c.father_comment and c.article_id = d.art_id\n" +
            "and article_id = #{artId} order by time asc")
    List<CommonVO> selectArtCommons(Integer artId);

    @Select("select c.id,c.time,c.content,c.user_id as sender_id,\n" +
            "  a.username as sender_name,\n" +
            "  c.article_id,\n" +
            "  father_comment as receiver_id,\n" +
            "  b.username as receiver_name,\n" +
            "  d.title as article_name\n" +
            "from user a, user b ,comment c,article d\n" +
            "where a.id = c.user_id and b.id = c.father_comment and c.article_id = d.art_id\n" +
            "order by time desc LIMIT 5")
    List<CommonVO> selectNewestCommons();
}